<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules, genFileId, UploadFile, UploadFiles, UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
import useStore from '@/store';
import { props } from './stockinventoryorderimport';
defineOptions({ name: 'StockInventoryOrderFormImportForm' });
const _props = defineProps(props);
const tableRef = ref(null);
const loading = ref(false);
const loadingDownLoad = ref(false);
const view = ref(_props.window);
const fileList = ref([]);
const warehouse = ref(_props.window.params.warehouse);
const lensId = ref(_props.window.params.detail.lensId);
const lensCode = ref(_props.window.params.detail.lensCode);
const lensName = ref(_props.window.params.detail.lensName);
const xyTable = ref(_props.window.params.detail.xyTable);
const save = async () => {
  if (!xyTable.value) {
    ElMessage.error('请导入XY表!');
    return;
  }
  ElMessage.success('保存成功!');
  useStore.window.windowRemove(_props.window?.name);
  if (_props.window?.callBack) _props.window?.callBack(lensId.value, xyTable.value);
};
const showXYTable = () => {
  if (fileList.value.length <= 0) {
    ElMessage.error('请上传文件!');
    return;
  }
  let params = new FormData();
  params.append('file', fileList.value[0].raw);
  loading.value = true;
  window.$api.common.getXYTable(params).then(res => {
    xyTable.value = res.data;
    loading.value = false;
  });
};
const upload = ref<UploadInstance>();

const handleExceed: UploadProps['onExceed'] = files => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
};
const exportTemplate = () => {
  loadingDownLoad.value = true;
  let params = { warehouseId: warehouse.value.id, lensId: lensId.value };
  window.$api.stock.downLoadXYTemplate(params).then(res => {
    loadingDownLoad.value = false;
  });
};
const exportCurrentStock = () => {
  loadingDownLoad.value = true;
  let params = { warehouseId: warehouse.id, lensId: lensId.value };
  window.$api.stock.exportCurrentStock(params).then(res => {
    loadingDownLoad.value = false;
  });
};
onMounted(() => {
  //showXYTable();
});
</script>

<template>
  <windows-window :window="view" v-loading="loadingDownLoad">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save" split></windows-button>
        <windows-button icon="fluent-contract-down-left-28-regular" @click="exportTemplate">导出模版</windows-button>
        <windows-button icon="fluent-contract-down-left-28-regular" @click="exportCurrentStock">导出现时库存</windows-button>
      </windows-button-group>
    </template>
    <el-form label-width="80px" size="default" status-icon>
      <el-row>
        <el-col :span="12">
          <el-form-item label="仓库代码">
            <el-input disabled v-model="warehouse.code" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库名称">
            <el-input disabled v-model="warehouse.name" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="镜种代码">
            <el-input disabled v-model="lensCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="镜种名称">
            <el-input disabled v-model="lensName" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择文件">
            <el-upload ref="upload" style="width: 100%" drag :limit="1" :auto-upload="false" :on-exceed="handleExceed" v-model:file-list="fileList">
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">拖动文件到此或者点击选择文件</div>
            </el-upload>
            <el-button type="success" icon="UploadFilled" @click="showXYTable">导入</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table ref="tableRef" :data="xyTable" style="width: 100%" height="400" highlight-current-row v-loading="loading" border>
            <el-table-column prop="sph" label="SPH" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="000" label="000" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-025" label="-025" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-050" label="-050" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-075" label="-075" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-100" label="-100" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-125" label="-125" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-150" label="-150" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-175" label="-175" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-200" label="-200" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-225" label="-225" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-250" label="-250" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-275" label="-275" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-300" label="-300" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-325" label="-325" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-350" label="-350" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-375" label="-375" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-400" label="-400" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-425" label="-425" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-450" label="-450" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-475" label="-475" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-500" label="-500" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-525" label="-525" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-550" label="-550" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-575" label="-575" align="center" minWidth="80" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-600" label="-600" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="total" label="Total" align="center" minWidth="60" show-overflow-tooltip fixed="right"></el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
  .el-upload-dragger {
    padding: 0;
  }
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
